package com.example.administrator.daggerdemo.utils;


import com.google.gson.Gson;

import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by lixiang on 16-5-27.
 */
public class StringUtils {
    private static Gson gson = new Gson();



    /**
     * 时间按照24小时制在前加零或不加
     * @param time
     * @return
     */
    public static String getFitTime(String time)
    {
        String result = time;
        if (time.length() == 1)
        {
            result = "0"+time;
        }
        return result;
    }
    /**
     * MD5加密
     *
     * @param str
     * @return
     */
    public static String MD5(String str) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        char[] charArray = str.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    public static boolean isTelphone(String telphone)
    {
        Pattern pattern = Pattern.compile("\\d{11}");
        Matcher matcher = pattern.matcher(telphone);
        return matcher.matches();
    }

    /**
     *
     * 根据正则截取字符串
     * @param str   待截取字符串
     * @param re    正则表达式
     * @return      如果匹配多个只返回第一个，如果没有匹配的返回null
     */
    public static String getStringByRegularE(String str,String re)
    {
        Pattern pattern = Pattern.compile(re);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find())
        {
            return matcher.group();
        }
        return null;
    }

    public static boolean isEmpty(String judge){
        boolean isEmpty = false;
        if (judge == null||judge.length()==0)
        {
            isEmpty = true;
        }
        return isEmpty;
    }


    public static boolean containsEmoji(String source) {


        return false;
    }

    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) ||
                (codePoint == 0x9) ||
                (codePoint == 0xA) ||
                (codePoint == 0xD) ||
                ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
                ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }
}
